package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService service;


    @RequestMapping("/getOrderSettingByMonth.do")
    public List<Map> getOrderSettingByMonth(String date) {

        List<Map> list = service.getOrderSettingByMonth(date);

        return list;

    }


    @RequestMapping("/editNumberByDate.do")
    public Result editNumberByDate(@RequestBody  OrderSetting os) {
        try {

            service.editNumberByDate(os);

        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);

    }





    @RequestMapping("/upload.do")
    public Result upload(MultipartFile excelFile) throws IOException, ParseException {

        try {
            //将行转换成list将单元格转换成string数组
            List<String[]>     list  = POIUtils.readExcel(excelFile);
            List<OrderSetting> array = new ArrayList<>();

            for(String[] str : list) {

                OrderSetting os = new OrderSetting();
                os.setOrderDate(new Date(str[0]));
                os.setNumber(Integer.parseInt(str[1]));

                array.add(os);

            }

            service.add(array);


        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

}
